package T回溯;

import java.util.LinkedList;

/**
 * @author tmh
 * @date 2023/10/12 20:59
 * @description
 */
public class T77_组合 {
    LinkedList<LinkedList<Integer>> result = new LinkedList<>();
    LinkedList<Integer> list = new LinkedList<>();

    public LinkedList<LinkedList<Integer>> combine(int n, int k) {
        combineHelper(n, k, 1);
        return result;
    }

    private void combineHelper(int n, int k, int startIndex) {
        //终止条件
        if (list.size() == k) {
            //这里要new一个集合
            result.add(new LinkedList<>(list));
            return;
        }
        for (int i = startIndex; i <= n; i++) {
            list.add(i);
            //下一层的startIndex要比上一层多1，所以这里是i+1
            combineHelper(n, k, i + 1);
            list.removeLast();
        }
    }

}
